#pragma once
/*
 * Created by: Mark Lavrynenko
 * Email:	MarkLavrinenko@gmail.com
 * Project:	  Huffman
*/

#include "HuffmanOptions.h"
#include <stdlib.h>
#include <assert.h>

typedef struct _node
{
public:
	_node(uint8_t code, mytype amount) : parent(nullptr), left(nullptr), right(nullptr),
		m_subtreeSize(1)
	{
		value = amount;
		letter = code;
	}

	// unite two other nodes to one
	_node(_node* leftChild, _node* rightchild): parent(nullptr), left(leftChild), right(rightchild)
	{
		assert(nullptr != left);
		assert(nullptr != right);
		value = left->value + right->value;
		m_subtreeSize = left->m_subtreeSize + right->m_subtreeSize + 1;
	}

	~_node()
	{
		if (NULL != left)
			delete left;
		if (NULL != right)
			delete right;
	}

	mytype value;
	uint8_t letter;
	// subtreeSize with element itself
	size_t m_subtreeSize;
	struct _node *left, *right, *parent;
	inline bool operator<(struct _node const& other) const
	{
		return (value < other.value) || (value == other.value && m_subtreeSize < other.m_subtreeSize);
	}
} HuffmanTreeNode;

